﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SWEN_HMS.Classes;
using System.Collections;

namespace SWEN_HMS.BookingModule
{
    public partial class CheckInSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else
            {
                time.Text = DateTime.Now.ToString();
            }
        }

        protected void checkIn_Click(object sender, EventArgs e)
        {
            //if nothing is selected from the gridview
            if (GridView1.SelectedRow == null)
            {
                System.Windows.Forms.MessageBox.Show("Please select the guest booking");
            }
            else
            {
                //var now is the current real time date
                var now = DateTime.Now.Date;

                //var first is the checkIn date
                var first = Convert.ToDateTime(checkInDate.Text);

                //var last is the checkOut date
                var last = Convert.ToDateTime(checkOutDate.Text);

                //if checkIn date is more or equal to the current date AND the checkOut date is more than or equal to the current date
                if (first <= now && now <= last)
                {
                    //update the checkInStatus to True in the Booking table
                    RoomDBManager.updateCheckInStatus(Convert.ToInt32(bookingID.Text));

                    //create/generate the invoiceID upon changing the checkInStatus 
                    RoomDBManager.createInvoiceID(Convert.ToInt32(bookingID.Text));

                    //update the checkInTime in the Room table
                    RoomDBManager.updateCheckInTime(Convert.ToInt32(roomNo.Text));

                    System.Windows.Forms.MessageBox.Show("Guest has been checked in");
                    Response.Redirect("~/BookingModule/RoomAvailability.aspx");
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Can't Check In Room");
                }
            }
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedRow != null)
            {
                RoomDBManager.deleteBooking(Convert.ToInt32(bookingID.Text));
                RoomDBManager.updateRoomAvailability(Convert.ToInt32(roomNo.Text));
                System.Windows.Forms.MessageBox.Show("Done");
                Response.Redirect("~/BookingModule/RoomAvailability.aspx");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Please select the guest booking");
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if the GridView is selected
            if (GridView1.SelectedRow != null)
            {
                //store everything into the GridViewRow
                GridViewRow row = GridView1.SelectedRow;

                checkInDate.Text = row.Cells[6].Text;
                checkInDate.Visible = true;
                checkOutDate.Text = row.Cells[7].Text;
                checkOutDate.Visible = true;
                bookingID.Text = row.Cells[1].Text;
                bookingID.Visible = true;
                roomNo.Text = row.Cells[2].Text;
                roomNo.Visible = true;
            }
        }

        protected void search_Click(object sender, EventArgs e)
        {
            //check the booking of the guest based on the guest first & last name
            Booking b = RoomDBManager.checkForBooking(firstname.Text, lastname.Text);

            if (b.firstName == firstname.Text && b.lastName == lastname.Text)
            {
                if (b.checkInStatus == "False")
                {
                    SqlDataSource1.SelectCommand = "Select * from Booking WHERE firstName Like'" + firstname.Text + "'AND checkInStatus='" + "False" + "'AND lastName Like'" + lastname.Text + "%'";
                    MultiView1.ActiveViewIndex = 1;
                    first.Text = b.firstName.ToString();
                    last.Text = b.lastName.ToString();
                    contactNo.Text = b.contactNo.ToString();
                }
                else
                {
                    error.Visible = true;
                    error.Text = "No guest booking has been made";
                }
            }
            else
            {
                error.Visible = true;
                error.Text = "Guest booking inforamtion does not exist";
            }
        }

        protected void cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/BookingModule/RoomAvailability.aspx");
        }

    }
}